11 / 14

Implement useLocalStorage hook.

javascript
Difficulty: 6/10
Topics: custom hook, localStorage API, state sync

Scenario Questions

0-2 years experience
  1. 1

    Imagine you need a simple form that remembers the user's name between page reloads. How would you use a useLocalStorage hook to achieve that?

  2. 2

    If you call your hook with a key that doesn't exist yet in localStorage, what value should it return and why?

2-5 years experience
  1. 1

    You're adding a dark‑mode toggle that should persist across sessions. Walk me through how you'd integrate a useLocalStorage hook, and what you would do if the stored value becomes corrupted JSON.

  2. 2

    During testing you notice the component re‑renders twice on mount when using the hook. What could be causing that and how would you fix it?

5-8 years experience
  1. 1

    Our app runs in multiple browser tabs and we need the theme preference to stay consistent. How would you extend the useLocalStorage hook to listen for changes from other tabs without causing performance issues?

  2. 2

    We have a feature flag stored in localStorage that many components read. Discuss how you would avoid stale reads and ensure updates propagate efficiently.

8+ years experience
  1. 1

    The team wants to replace localStorage with a centralized user‑settings service while keeping the existing hook API. How would you design the abstraction to support both storage mechanisms and minimize impact on downstream components?

  2. 2

    Considering security and GDPR, what architectural concerns arise from persisting user data in localStorage, and how would you mitigate them at a system level?

Follow-up Questions

  • How would you handle a situation where localStorage is disabled or throws?
  • What would you change to keep multiple tabs in sync when the value changes?
  • Can you discuss the trade‑offs of storing large objects versus primitives?